package com.kun.ssm.demo.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kun.admin.authority.service.AuthorityUserService;
import com.kun.base.model.Pager;
import com.kun.base.model.UserAndInfoModel;
import com.kun.base.web.model.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by admin on 2017/11/8.
 * demo controller
 */
@Controller
@RequestMapping("/demo")
@Api(value = "/demo", description = "示例接口")
public class DataTableController {

    @Autowired
    AuthorityUserService authorityUserService;

    /**
     * 数据表格数据查询
      * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getDataTableList")
    @ApiOperation(value = "返回表格数据", notes = "返回表格显示的json数据")
    @ApiImplicitParam(name = "offset", value = "偏移数据量", required = true, dataType = "int")
    public Pager getDataTableList(int offset,int limit){
        PageHelper.startPage(offset/limit+1, limit);
        Page<UserAndInfoModel> userList= authorityUserService.list();
        return new Pager<UserAndInfoModel>(userList);
    }

    @ResponseBody
    @RequestMapping(value="/uploadFile")
    public AjaxResult name(@RequestParam(value = "demoFile", required = false) MultipartFile file[]) throws IOException {
        if (file!=null && file.length>0){
            for (int i = 0; i < file.length; i++) {
                File f=new File("D:/fileupload/"+System.currentTimeMillis()+file[i].getOriginalFilename());
                if (!f.getParentFile().exists()){
                    f.getParentFile().mkdirs();
                }
                FileOutputStream fos=new FileOutputStream(f);
                InputStream is=file[i].getInputStream();
                IOUtils.copy(is,fos);
                fos.close();
            }
        }
        return new AjaxResult(true);
    }
}
